<?php
class Con_anmp extends Acan{

	public $defaultAct='host';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){
			$this->id = '0';
		}
		
		$this->nginx_host = str_replace("\\","/",Acan_anmp_path.'/config/nginx/host/');
		$this->nginx_rewrite = Acan_anmp_path.'/config/nginx/rewrite/';
		$this->nginx_logs = Acan_anmp_path.'/temp/alogs/';
		$this->path_mysql = Acan_anmp_path.'/config/mysql/';
		$this->afile = Afunc_file();
	}

	function Act_log_lists(){
		$logs = $this->afile->ascandir($this->nginx_logs);
		include $this->Atpl();
	}
	function Act_log_list(){
		$dates = $_GET['date'];
		if(empty($dates)){Afun_refresh('请先选择日期','/'.$this->Amodr.'-log_lists');}
		$logs = $this->afile->ascanfileinfo($this->nginx_logs.$dates.'/');
		include $this->Atpl();
	}
	function Act_log_del(){
		unlink($this->nginx_logs.$_GET['path']);
		Afun_refresh('删除成功','referrer');
	}
	function Act_log_dels(){
		Afun_file_rmdirm($this->nginx_logs.$_GET['date'].'/');
		Afun_refresh('删除成功','referrer');
	}

	function Act_nginx_reload(){
		$rdate = date('H:i',time()+60);
		$cmd = 'at /delete /yes';
		echo Afun_run_cmd(`$cmd`);
		$cmd = "at ".$rdate.' "E: & cd '.Acan_anmp_path.' & cd run & nginx_reload.cmd"';
		echo Afun_run_cmd($cmd);
		Afun_refresh('nginx将在'.$rdate.'重载','referrer');
	}
	function Act_host(){
		$host = $this->afile->ascanfileinfo($this->nginx_host);
		//print_r($host);
		include $this->Atpl();
	}
	function Act_host_start(){
		$filepath = $this->nginx_host.$_GET['name'];
		if($this->id=='1'){
			rename($filepath.'.conf2',$filepath.'.conf');
		}else{
			rename($filepath.'.conf',$filepath.'.conf2');
		}
		Afun_refresh('修改成功','referrer');
	}
	function Act_host_up(){
		$form_radio_isorno = array('1'=>'启用','2'=>'关闭','3'=>'不存在');
		$form_radio_onoff = array('on'=>'开启','off'=>'禁用');
		$host = $this->afile->aedit_view($this->nginx_host.$_GET['file']);
		$host = str_replace("\r\n","\n",$host);
		$data = $this->host_list($host);
	//	print_r($data);
		if($_POST){
			$this->host_save($data,$host);
		}
		include $this->Atpl();
	}
	function Act_host_del(){
		unlink($this->nginx_host.$_GET['file']);
		Afun_refresh('删除成功','referrer');
	}
	function Act_rewrite(){
		$host = $this->afile->ascanfileinfo($this->nginx_rewrite);
		include $this->Atpl();
	}
	function Act_rewrite_up(){
		if($_POST['save']['note']){
			$_POST['save']['note'] = str_replace('\\\\','\\',$_POST['save']['note']);
			if(!empty($_POST['copy']['name'])){
				$filename = $_POST['copy']['name'];
				if(!preg_match('/\.conf/i',$filename)){
					$filename = $filename.'.conf';
				}
			}else{
				$filename = $_GET['file'];
			}
			$this->afile->file_save($this->nginx_rewrite.$filename,$_POST['save']['note']);
			if(!empty($_POST['copy']['name'])){
				Afun_refresh('复制新伪静态成功','/'.$this->Aactr.'?file='.$filename);
			}
			Afun_refresh('修改成功','referrer');
		}
		$note = $this->afile->aedit_view($this->nginx_rewrite.$_GET['file']);
		$note = str_replace("\r\n","\n",$note);
		include $this->Atpl();
	}
	function Act_rewrite_del(){
		unlink($this->nginx_rewrite.$_GET['file']);
		Afun_refresh('删除成功','referrer');
	}
	function host_list($host){
		preg_match_all('/server {([\w\s\d\x{4e00}-\x{9fa5}\\\.\/\|$ ;{}()?*-~#])+}#&/iu',$host,$info);
		if(empty($info[0][0])){
			$host = Afun_charset($host);
			preg_match_all('/server {([\w\s\d\x{4e00}-\x{9fa5}\\\.\/\|$ ;{}()?*-~#])+}#&/iu',$host,$info);
			if(empty($info[0][0])){return ;}
		}
		foreach ($info[0] as $key => $val) {
			$data[] = $this->host_info($val);
		}
		return $data;
	}
	function host_info($note){
		preg_match_all('/([#]*?)\tlisten ([\w\d\s]+);/i',$note,$data['listen']);
		preg_match_all('/([#]*?)\tserver_name ([\w\d\s\.* -]+);/i',$note,$data['server_name']);
		preg_match_all('/([#]*?)\troot ([\w\d\s\.\/ -:]+);/i',$note,$data['root']);
		preg_match_all('/([#]*?)\tindex ([\w\d\s\. -]+);/i',$note,$data['index']);
		preg_match_all('/([#]*?)\tinclude aphp.conf;/i',$note,$data['php']);
		preg_match_all('/([#]*?)\tinclude ssi.conf;/i',$note,$data['ssi']);
		preg_match_all('/([#]*?)\tautoindex (on|off);/i',$note,$data['autoindex']);
		preg_match_all('/([#]*?)\taccess_log logs\/([\w\d\s\.\/ -]+).log alog;/i',$note,$data['access_log']);
		preg_match_all('/([#]*?)\tinclude rewrite\/([\w\d\s\.-]+)\.conf;/i',$note,$data['rewrite']);
		foreach ($data as $k => $v) {
			$data[$k] = $this->host_info_use($v);//判断是否启用
		}
		$data['server_name'] = $this->host_info_domain($data['server_name']);
		return $data;
	}
	function host_info_domain($obj){
		if(empty($obj['val'])){
			return ;
		}
		$urr = explode(" ",$obj['val']);
		foreach ($urr as $k => $v) {
			$html .= "<a href=\"http://$v\" target=\"_blank\">$v</a>";
		}
		$obj['url'] = $html;
		return $obj;
	}
	function host_info_use($obj){
		if(!empty($obj[0][0])){
			if(!empty($obj[1][0])){
				$obj['use']='2';//关闭
			}else{
				$obj['use']='1';//启用
			}
			$obj['val'] = $obj[2][0];
			$obj['all'] = $obj[0][0];
		}else{
			$obj['use']='3';//未找到
		}
		return $obj;
	}
	function host_save($data,$host){
		foreach($_POST['save'] as $key=>$val){
			foreach ($val as $k => $v) {
				if(!empty($v['val'])){
					$newstr = '2';
					if(!empty($data[$key][$k]['all'])){
						$str0 = $data[$key][$k]['all'];
					}else{
						$newstr = '1';
						$str0 = "\n}#&".$key;
					}
					if($newstr == '1'){//新插入
						$str_start = "\n";
						$str_end = $str0;
					}else{
						$str_start = "";
						$str_end = "";
					}
					if($k=='access_log'){$v_start = $k.' '.'logs/';$v_end = '.log alog';}
					elseif($k=='rewrite'){$v_start = 'include rewrite/';$v_end='.conf';}
					elseif($k=='php'){$v_start = 'include aphp';$v_end='.conf';}
					elseif($k=='ssi'){$v_start = 'include ssi';$v_end='.conf';}
					else{$v_start = $k.' ';$v_end='';}
					if($v['use']){
						if($v['use']==1){
							$str = "\t".$v_start.$v['val'].$v_end.';';
						}elseif($v['use']==2){
							$str = "#\t".$v_start.$v['val'].$v_end.';';
						}else{
							$str = "";
						}
					}else{
						$str = "\t".$k.' '.$v['val'].';';
					}
					$str = $str_start.$str.$str_end;
				}
				$host = str_replace($str0,$str,$host);
				unset($str0,$str);
			}
		}
		$host = str_replace("\n\n","\n",$host);
		if(!empty($_POST['copy']['name'])){
			$filename = $_POST['copy']['name'];
			if(!preg_match('/\.conf/i',$filename)){
				$filename = $filename.'.conf';
			}
		}else{
			$filename = $_GET['file'];
		}
		$this->afile->file_save($this->nginx_host.$filename,$host);
		if(!empty($_POST['copy']['name'])){
			Afun_refresh('复制新主机成功','/'.$this->Aactr.'?file='.$filename);
		}else{
			Afun_refresh('修改成功','referrer');
		}
	}
	function host_save_str(){
		return ;
	}
}

?>